package 二叉树;

public class LC_101对称二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        //后序遍历：左右中;判断左右子树是否可以翻转
        return root==null || compare(root.left,root.right);
    }
    public static boolean compare(TreeNode left,TreeNode right){
        if(left==null && right==null) return true;
        else if(left==null && right!=null) return false;
        else if(left!=null && right==null) return false;
        else if(left.val!= right.val) return false;
        boolean outside=compare(left.left,right.right);
        boolean inside=compare(left.right,right.left);
        boolean result=outside && inside;
        return result;
    }
}
